home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK1.toast / Development Kits (Disc 1) / QuickDraw GX / Programming Stuff / Sample Code / Typography Samples / Spiral Layout ƒ / Spiral Layout.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-06-18  |  15.1 KB  |  483 lines  |  [TEXT/KAHL]

  1. /**\
  2. |**| =====================================================================
  3. |**|
  4. |**|    Spiral Layout.c
  5. |**|
  6. |**|    This file contains the calls that this sample needs to make
  7. |**|    the QuickDraw GX shell work correctly.
  8. |**|
  9. |**|    QuickDraw GX Libraries Used:
  10. |**|    "ColorLibrary.c", "FontLibrary.c", "GraphicsDebugLibrary.c",
  11. |**|    "LayoutLibrary.c", "ShapeLibrary.c", and "TransformLibrary.c".
  12. |**|
  13. |**|    6/96 bob    Updated #includes to support changed GX Library names.
  14. |**|                Updated the copyright date.
  15. |**|
  16. |**|    ©1990 - 1996  Apple Computer, Inc.
  17. |**|    All rights reserved.
  18. |**|
  19. |**| =====================================================================
  20. \**/
  21.  
  22.  
  23. #include "QDGX shell.h"
  24. #include <GXLayout.h>
  25. #include "LayoutLibrary.h"
  26.  
  27.  
  28. /**\
  29. |**| ---------------------------------------------------------------------
  30. |**| PROTOTYPES
  31. |**| ---------------------------------------------------------------------
  32. \**/
  33.  
  34. // funtions required by shell
  35.  
  36. void    DoSetup            (void);
  37. void    DoDraw            (WindowPtr wind, Boolean updating);
  38. OSErr    DoCreateNew        (void);
  39. void    DoDispose        (WindowPtr wind);
  40. void    DoIdle            (WindowPtr wind);
  41. void    DoTeardown        (void);
  42. void    DoClick            (WindowPtr wind, Point p);
  43.  
  44. // private functions
  45.  
  46. OSErr    DoWindowInit        (WindowPtr wind);
  47. void    CreateSampleImage    (WindowPtr wind);
  48.  
  49.  
  50. /**\
  51. |**| ---------------------------------------------------------------------
  52. |**| ENUMS
  53. |**| ---------------------------------------------------------------------
  54. \**/
  55. enum { rWindResource = 128 };
  56.  
  57.  
  58. /**\
  59. |**| ---------------------------------------------------------------------
  60. |**| GLOBALS
  61. |**| ---------------------------------------------------------------------
  62. \**/
  63. // If gDebugging = TRUE, graphics library errors and notices will be posted.  This
  64. // functionality will only work with the "debugging" version of QuickDraw GX.
  65. // If the debugging version is not installed, nothing bad will happen, but these
  66. // functions will not work. 
  67.  
  68. Boolean        gDebugging = true;
  69.  
  70. // Set  "gGiveMeValidation" to TRUE if you want receive run-time validation.
  71.  
  72. Boolean        gGiveMeValidation = true;
  73.  
  74.  
  75. // gGraphicsHeapSize sets the size of the graphics heap created by calling the
  76. // GXNewGraphicsClient routine in main () within QuickDraw GX shell.c.  You can determine
  77. // the amount of graphics heap required by using GraphicsBug.  I'm giving it 600K,
  78. // since we need abunch if we have several windows open.
  79.  
  80. long        gGraphicsHeapSize = 600;
  81.  
  82. // gOurPrintingOverrideUPP is a universal proc pointer for our printing event
  83. // override.  This is so that our override can be native PowerPC code if necessary.
  84.  
  85. GXPrintingEventUPP    gOurPrintingOverrideUPP;
  86.  
  87.  
  88.  
  89. /**\
  90. |**| ---------------------------------------------------------------------
  91. |**| DoSetup()
  92. |**| Here's where we initialize any global variables our application needs.
  93. |**| We have only one at this time -- the universal proc pointer for
  94. |**| our printing override.
  95. |**| ---------------------------------------------------------------------
  96. \**/
  97. void DoSetup (void)
  98. {    // Initialize our printing event override UPP
  99.     gOurPrintingOverrideUPP = NewGXPrintingEventProc(MyPrintingEventOverride);
  100. }
  101.  
  102.  
  103. /**\
  104. |**| ---------------------------------------------------------------------
  105. |**| DoDraw()
  106. |**| Draw the contents of the window.  The first parameter is the window
  107. |**| to draw, and the second parameter is true if we're updating an existing
  108. |**| image.  If that's the case, we don't want to change anything, but
  109. |**| just draw what's already there.
  110. |**| ---------------------------------------------------------------------
  111. \**/
  112. void DoDraw (WindowPtr wind, Boolean updating)
  113. {
  114.      #pragma unused (updating)
  115.      GXDrawShape (GetDocShape(wind));
  116. }
  117.  
  118.  
  119. /**\
  120. |**| ---------------------------------------------------------------------
  121. |**| DoCreateNew()
  122. |**| This routine is called when a window needs to be created.
  123. |**| ---------------------------------------------------------------------
  124. \**/
  125. OSErr DoCreateNew (void)
  126. {
  127.     OSErr        err = noErr;
  128.     WindowPtr    wind;
  129.     
  130. // Get and create our window from the resource fork
  131.  
  132.     wind = GetNewWindow(rWindResource, nil, (WindowPtr)-1L);
  133.  
  134. // Attach a default gxViewPort to it, create and iInitialize our
  135. // private data for it, and add a sample image to its page shape.
  136.  
  137.     if ( wind == NULL )
  138.         return (MemError());
  139.  
  140.     GXIgnoreGraphicsNotice(transform_already_set);
  141.     SetDefaultViewPort(GXNewWindowViewPort(wind));            
  142.     GXPopGraphicsNotice();
  143.     
  144.     err = DoWindowInit(wind);
  145.     if ( err != noErr )
  146.         return err;
  147.     
  148.     CreateSampleImage(wind);
  149.     return err;
  150. }
  151.  
  152.  
  153. /**\
  154. |**| ---------------------------------------------------------------------
  155. |**| DoDispose()
  156. |**| This routine is called when a window needs to be disposed of.
  157. |**| ---------------------------------------------------------------------
  158. \**/
  159. void DoDispose (WindowPtr wind)
  160. {
  161.     TH_Doc    doc;
  162.     
  163. // You should always dispose of your GX graphics objects before tossing your window.
  164. // Why?  It's generally good form and this approach guarantees that everything is
  165. // disposed.  If you had not disposed of everything, the call to DisposeWindow should
  166. // dispose of the objects. If you are running the debugging version of QuickDraw GX
  167. // with notices set, you will receive a notice that you had not disposed of everything.
  168. // You can turn notices on in this file by setting gDebugging = TRUE (above).
  169.     
  170.     if ( wind != NULL )
  171.     {
  172.         doc = (TH_Doc)GetWRefCon(wind);        // Remember, this is where we stored our private data.
  173.         GXDisposeShape(GetDocShape(wind));     // Dispose of this doc's shape.
  174.         GXDisposeJob(GetDocJob(wind));        // Dispose of this doc's print job.
  175.         DisposHandle((Handle) doc);            // Dispose of our private data.
  176.         DisposeWindow(wind);                // Dispose of the window.
  177.     }
  178. }
  179.  
  180.  
  181. /**\
  182. |**| ---------------------------------------------------------------------
  183. |**| DoIdle()
  184. |**| This routine is called to do things while idling through the event loop.
  185. |**| ---------------------------------------------------------------------
  186. \**/
  187. void DoIdle (WindowPtr wind)
  188. {
  189. }
  190.  
  191.  
  192. /**\
  193. |**| ---------------------------------------------------------------------
  194. |**| DoTeardown()
  195. |**| This routine is called just before we quit to remove anything 
  196. |**| persistent that might have been setup by DoSetup().
  197. |**| ---------------------------------------------------------------------
  198. \**/
  199. void DoTeardown (void)
  200. {
  201.     DisposeRoutineDescriptor(gOurPrintingOverrideUPP);
  202. }
  203.  
  204.  
  205. /**\
  206. |**| ---------------------------------------------------------------------
  207. |**| DoClick()
  208. |**| ---------------------------------------------------------------------
  209. \**/
  210. void DoClick(WindowPtr window, Point p)
  211. {
  212. }
  213.  
  214.  
  215.  
  216.  
  217.  
  218. /**\
  219. |**| ---------------------------------------------------------------------
  220. |**| DoWindowInit()
  221. |**| In this function we create and initialize the the private document
  222. |**| structure for a new window.  This structure contains the print job and
  223. |**| the shape which is drawn in the window.  We store this data in a handle
  224. |**| and hang it off the window's refCon field for easy retrieval.  By doing
  225. |**| this, rather than using globals, we can create many windows containing
  226. |**| unique print jobs and shapes.
  227. |**| ---------------------------------------------------------------------
  228. \**/
  229. OSErr DoWindowInit (WindowPtr wind)
  230. {
  231.     OSErr    err = noErr;
  232.     gxJob    docJob;
  233.     gxShape    docPage;
  234.     TH_Doc    windDoc;
  235.  
  236.  
  237. // Create the page shape. We set the unique items attribute to make sure that each item
  238. // added to the picture has a unique reference. If this attribute was not set, we would
  239. // not see all copies of anything we add to the shape multiple times -- we'd just see
  240. // the last version added.        
  241.  
  242.     docPage = GXNewShape(gxPictureType);
  243.     GXSetShapeAttributes(docPage, (GXGetShapeAttributes(docPage) | gxUniqueItemsShape));
  244.     
  245.     
  246. // Create a print job for this document.  This will be the same as the system default until
  247. // the user goes through the dialogs for Page Setup or Print…
  248.  
  249.     err = GXNewJob(&docJob);
  250.     
  251.     
  252. // If there are no errors, create a handle the size of our document structure and store
  253. // the print job and page shape in it.  Store the handle in the window's refCon field so
  254. // that we can get at it.  (Note that the utility routines "GetDocJob" and "GetDocShape"
  255. // can be used to do this easily.
  256.  
  257.     if ( err == noErr )
  258.     {
  259.         windDoc = (TH_Doc) NewHandleClear(sizeof(T_Doc));
  260.  
  261.         if ( windDoc == NULL )
  262.             err = MemError();
  263.         else
  264.         {
  265.             (*windDoc)->docJob = docJob;
  266.             (*windDoc)->docPage = docPage;
  267.             SetWRefCon(wind, (long) windDoc);
  268.         }
  269.  
  270. // Now install our application override for PrintingEvent so that we can
  271. // support the new movable-modal printing dialog boxes.
  272.  
  273.         GXInstallApplicationOverride(docJob, gxPrintingEventMsg, gOurPrintingOverrideUPP);
  274.  
  275.     }
  276.  
  277.     return err;
  278. }
  279.  
  280.  
  281. /**\
  282. |**| ---------------------------------------------------------------------
  283. |**| CreateSampleImage()
  284. |**| This function creates primitive shapes and adds them to the window's page shape.
  285. |**| ---------------------------------------------------------------------
  286. \**/
  287. void CreateSampleImage (WindowPtr wind)
  288. {
  289.     Rect    ourWindowRect = wind->portRect; // the rectangle for this window
  290.                                             // in QuickDraw coordinates
  291.     gxShape layout;                        // the layout shape we build
  292.     gxShape thePage;                    // this window's document shape
  293.     gxRunControls runControls;            // run controls for the layout shape
  294.     gxLayoutOptions layoutOptions;        // options for the layout shape
  295.     gxStyle timesStyle;                    // a style record for a Times-based style
  296.     gxStyle helveticaStyle;                // ditto for Helvetica
  297.     gxStyle baghdadStyle;                 // ditto for Baghdad
  298.     gxStyle hoeflerStyle;                 // ditto for Hoefler
  299.     gxStyle textStyles[4];                // an array of text style for our text runs
  300.     
  301. // These are the four text runs for this layout shape, in pieces because they're
  302. // in different languages and styles
  303.     
  304.     char *text1 = "Aetna ";
  305.     
  306. // The following is "Macintosh" in Arabic: 
  307. // meem, alif, kaf,  noon, tah,  wau,  shin
  308. // (This assumes the standard Arabic character set for Macintosh, by the way)
  309.     
  310.     static char text2[] = {0xE5, 0xC7, 0xE3, 0xE6, 0xCA, 0xE8, 0xD4, 0};
  311.     
  312.     char *text3 = " Office";
  313.     char *text4 =         " AWAY.";
  314.  
  315.     char *textRuns[4];                    // an array of pointers to text runs
  316.  
  317.     short textLengths[4];                // an array of the lengths of each run
  318.     short totalLength;                    // the total length of the layout's text
  319.     short level0 = 0;                    // so we can take the address of it later
  320.     
  321.     gxPoint posn;                        // the position of the layout shape
  322.     
  323.     gxColor ourColor;                    // the color we'll use
  324.         
  325.     short    counter;                    // loop variable
  326.     gxTransform ourTransform;            // transform for our layout shape
  327.  
  328.  
  329. // First, initialize the textRuns array to point to the four text runs
  330.     
  331.     textRuns[0] = text1;
  332.     textRuns[1] = text2;
  333.     textRuns[2] = text3;
  334.     textRuns[3] = text4;
  335.     
  336.     
  337. // Next, initialize the textLengths and levelRunLengths arrays.  MyStrLength() is
  338. // in this file.
  339.  
  340.     textLengths[0] = MyStrLength (text1);
  341.     textLengths[1] = MyStrLength (text2);
  342.     textLengths[2] = MyStrLength (text3);
  343.     textLengths[3] = MyStrLength (text4);
  344.     
  345. // totalLength is the length of all the text.
  346.  
  347.     totalLength = textLengths[0] + textLengths[1] + textLengths[2] + textLengths[3];
  348.     
  349.  
  350. // make default gxLayoutOptions and gxRunControls structures
  351.  
  352.     InitializeLayoutOptions (&layoutOptions);
  353.     InitializeRunControls (&runControls);
  354.     
  355.     
  356. // Put the start of the layout in the center of the window
  357.  
  358.     posn.x = ff((ourWindowRect.right - ourWindowRect.left) / 2);
  359.     posn.y = ff((ourWindowRect.bottom - ourWindowRect.top) / 2);
  360.     
  361.  
  362. // Create the styles we'll use.  All styles are 24-point text.
  363. // NewLayoutStyle is a library routine found in layout library.c.
  364.  
  365.  
  366.     GXIgnoreGraphicsNotice(text_size_already_set);
  367.     helveticaStyle = NewLayoutStyle(
  368.         (char *) "\pHelvetica",                // the gxFontName for the style
  369.         ff(12),                                // the text size in fixed point
  370.         0,                                    // gxTextAttributes
  371.         &runControls,                        // run controls (our default ones)
  372.         nil,                                // run features (none for this style)
  373.         0,                                    // count of run features
  374.         nil);                                // style run overrides (none right now)
  375.     GXPopGraphicsNotice();
  376.  
  377.     GXIgnoreGraphicsNotice(text_size_already_set);
  378.     baghdadStyle = NewLayoutStyle(
  379.         (char *) "\pBaghdad Plain",            // gxFontName
  380.         ff(12),                                // text size (fixed point)
  381.         0,                                    // gxTextAttributes
  382.         &runControls,                        // run controls (our default ones)
  383.         nil,                                 // run features (none)
  384.         0,                                     // count of run features (none)
  385.         nil);                                // style run overrides (none)
  386.     GXPopGraphicsNotice();
  387.     
  388.     GXIgnoreGraphicsNotice(text_size_already_set);
  389.     timesStyle = NewLayoutStyle(
  390.         (char *) "\pTimes Roman",             // gxFontName
  391.         ff(12),                             // text size (fixed point)
  392.         0,                                    // gxTextAttributes
  393.         &runControls,                         // run controls (our default ones)
  394.         nil,                                 // run features (none)
  395.         0,                                     // count of run features (none)
  396.         nil);                                // style run overrides (none)
  397.     GXPopGraphicsNotice();
  398.     
  399.     GXIgnoreGraphicsNotice(text_size_already_set);
  400.     hoeflerStyle = NewLayoutStyle(
  401.         (char *) "\pHoefler Italic",             // gxFontName
  402.         ff(12),                             // text size (fixed point)
  403.         0,                                    // gxTextAttributes
  404.         &runControls,                         // run controls (our default ones)
  405.         nil,                                 // run features (none)
  406.         0,                                     // count of run features (none)
  407.         nil);                                // style run overrides (none)
  408.     GXPopGraphicsNotice();
  409.  
  410. // Initialize the textStyles array so that each of the five runs has the right style
  411.  
  412.     textStyles[0] = helveticaStyle;
  413.     textStyles[1] = baghdadStyle;
  414.     textStyles[2] = hoeflerStyle;
  415.     textStyles[3] = timesStyle;
  416.     
  417.  
  418. // Setup complete!  Build the layout.
  419.  
  420.     layout = GXNewLayout(
  421.         4,                                // count of text runs
  422.         textLengths,                    // array of lengths of each run
  423.         (void *) textRuns,                // array of pointers to the text
  424.         4,                                // count of style runs
  425.         textLengths,                    // array of the byte lengths of each run
  426.         textStyles,                        // array of the styles
  427.         1,                                // number of levels in the layout
  428.         &totalLength,                    // array of the lengths of each level
  429.         &level0,                        // array of the levels
  430.         &layoutOptions,                    // options (default)
  431.         &posn);                            // position of the layout shape
  432.         
  433. // Retrieve the page shape so we can add to it.
  434.  
  435.     thePage = GetDocShape(wind);
  436.     
  437. // Get the transform for our layout
  438.  
  439.     ourTransform = GXGetShapeTransform(layout);
  440.     
  441. // Initialize our color to red
  442.  
  443.     (void *) SetCommonColor(&ourColor, red);
  444.     (void *) GXConvertColor(&ourColor, gxHSVSpace, nil, nil);    // convert to HSV
  445.  
  446. // Now, rotate the layout 15 degrees 24 times for a total of 360 degrees.
  447. // Each rotation we'll also change the hue by 2730 and scale in both directions
  448. // by 17/16.    
  449.  
  450.     for (counter = 0; counter <= 24; counter++)
  451.     {
  452.         GXSetShapeColor(layout, &ourColor);
  453.         AddToShape(thePage, layout);
  454.         ourColor.element.hsv.hue += 0x0AAA;
  455.         GXRotateTransform(
  456.             ourTransform,                // the transform to spin
  457.             ff(15),                        // how many degrees to rotate
  458.             posn.x,                        // the x center of rotation
  459.             posn.y);                    // the y center of rotation
  460.         GXScaleTransform(
  461.             ourTransform,                // the transform to scale
  462.             (Fixed)0x00011000,            // scale horizontally by 17/16
  463.             (Fixed)0x00011000,            // scale vertically by 17/16
  464.             posn.x,                        // the x center of scaling
  465.             posn.y);                    // the y center of scaling
  466.     }
  467.  
  468. // Dispose of the shapes and styles we created
  469.  
  470.     GXDisposeShape(layout);
  471.     GXDisposeStyle(helveticaStyle);    
  472.     GXDisposeStyle(baghdadStyle);    
  473.     GXDisposeStyle(timesStyle);    
  474.     GXDisposeStyle(hoeflerStyle);    
  475.     
  476. // Invalidate the window's portRect so that everything gets updated.
  477.     
  478.     SetPort(wind);
  479.     InvalRect(&wind->portRect);
  480. }
  481.  
  482.  
  483.